//
//  MyTabBar.m
//  CustomTabbar
//
//  Created by simon on 10-9-14.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "MyTabBar.h"
#import "MyTabBarItem.h"
#import "CustomButton.h"
#import "MyViewController.h"
#import "MyTabBarController.h"
#import "RS3ViewController.h"
#import "RS1ViewController.h"
#import "Reachability.h"

@implementation MyTabBar
@synthesize leftMargin, rightMargin, topMargin, bottomMargin, itemSpacing, imageRatio, titleFont, delegate,items, tabBarController;


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        items = [[NSArray alloc] init];
		buttons = [[NSMutableArray alloc] init];
		//titleFont = [UIFont systemFontOfSize:12];
		appDelegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    return self;
}


-(void)setItems:(NSArray*)itemsPara{
	int count = [itemsPara count];
	for(int i=0; i<[itemsPara count]; i++){		
		MyTabBarItem* item = (MyTabBarItem*)[itemsPara objectAtIndex:i];
		item.index = i;
		
		float width = (self.frame.size.width - leftMargin - rightMargin - itemSpacing*(count-1))/[itemsPara count];
		float height = self.frame.size.height - topMargin - bottomMargin;
		float x = leftMargin + (width+itemSpacing)*i;
		float y = topMargin;
		
		//imageView
		float imageViewHeight = height*imageRatio;
		UIView* imageView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, imageViewHeight)];
		
		//custom button
		CustomButton* imageButton = [CustomButton buttonWithType:UIButtonTypeCustom];
		imageButton.frame = CGRectMake(0, 0, imageViewHeight, imageViewHeight); //button是正方形的	
		imageButton.center = CGPointMake(width/2, imageViewHeight/2);
		imageButton.item = item;
		[imageButton addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
		[imageView addSubview:imageButton];
		[self addSubview:imageView];
		[buttons addObject:imageButton];
		
		//titleLabel
		CGRect labelFrame = CGRectMake(x, y+imageViewHeight, width, height-imageViewHeight);
		UILabel* titleLabel = [[UILabel alloc] initWithFrame:labelFrame];
		titleLabel.text = item.title;
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.textAlignment = UITextAlignmentCenter;
		titleLabel.font = [UIFont boldSystemFontOfSize:12];
		[self addSubview:titleLabel];
	}
	
}


-(void) pressAction:(id)sender{
	CustomButton* currentButton = (CustomButton*)sender;
	

	
	if (currentButton.item.index == 4) {
		
		Reachability *internetReach = [Reachability reachabilityForInternetConnection];
		[internetReach startNotifier];	
		NetworkStatus netStatus = [internetReach currentReachabilityStatus];
		
		if (!netStatus) {
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
														   delegate:nil cancelButtonTitle:@"确定"
												  otherButtonTitles:nil];
			[alert show];
			[alert release];	
		}
		else {
			currentButton.selected = YES;
			for (CustomButton* button in buttons) {
				if(button != currentButton){
					button.selected = NO;
				}
			}
			[tabBarController setContentView:currentButton.item.viewController.view];
			
//			if (currentButton.item.index == 0) {
//				[appDelegate.rs3VC viewWillShow];
//			}else if (currentButton.item.index == 1) {
//				[appDelegate.rs1VC viewDidShow];
//			}else if (currentButton.item.index == 4) {
				[appDelegate.rs2VC viewDidShow];
//			}
		}
	}
	else {
		currentButton.selected = YES;
		for (CustomButton* button in buttons) {
			if(button != currentButton){
				button.selected = NO;
			}
		}
		[tabBarController setContentView:currentButton.item.viewController.view];
		
		if (currentButton.item.index == 0) {
			[appDelegate.rs3VC viewWillShow];
		}else if (currentButton.item.index == 1) {
			[appDelegate.rs1VC viewDidShow];
		}else if (currentButton.item.index == 4) {
			[appDelegate.rs2VC viewDidShow];
		}
		
	}
	
	
	NSLog(@" = %d",currentButton.item.index);
}


-(void)setAllButtonUnselected{
	for (CustomButton* button in buttons) {		
		button.selected = NO;
	}
}

-(void)setSelectedAtIndex:(int)index{
	for(int i=0; i<[buttons count]; i++){
		CustomButton* button = [buttons objectAtIndex:i];
		if(i == index){			
			button.selected = YES;
		}if(i != index) {
			button.selected = NO;
		}
	}
}

- (void)dealloc {
    [tabBarController release];
	[items release];
    [super dealloc];
}

@end
